<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称/菜单路径" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreateZi()">新建子菜单</el-button>
                <el-button type="primary" class="butT" @click="handleCreateFu()">新建父菜单</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="linkUrl" label="菜单路径" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.linkUrl == null ? '/' : scope.row.linkUrl}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.name == null ? '未填写' : scope.row.name}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="level" label="菜单级别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.level == '0' ? '不限' : scope.row.level == '1' ? '父菜单' : '子菜单'}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增子菜单 -->
            <div class="add-form">
                <el-dialog title="新增菜单项" :visible.sync="dialogFormVisibleZi">
                    <el-form ref="dataAddFormZi" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单栏位置" prop="menuPosition">
                                    <el-select v-model="formData.parentMenuId">
                                        <el-option
                                                v-for="item in items"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleZi = false">取消</el-button>
                        <el-button type="primary" @click="handleAddZi()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--新增父菜单-->
            <div class="add-form">
                <el-dialog title="新增父菜单" :visible.sync="dialogFormVisibleFu">
                    <el-form ref="dataAddFormFu" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleFu = false">取消</el-button>
                        <el-button type="primary" @click="handleAddFu()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑子标签 -->
            <div class="add-form">
                <el-dialog title="编辑子菜单" :visible.sync="dialogFormVisible4EditZi">
                    <el-form ref="dataEditFormZi" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单级别">
                                    <el-select v-model="formData.level">
                                        <el-option label="子菜单" value="2"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父菜单">
                                    <el-select v-model="formData.parentMenuId">
                                        <el-option
                                                v-for="item in items"
                                                :label="item.name"
                                                :value="item.id">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEditZi()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑父标签 -->
            <div class="add-form">
                <el-dialog title="编辑父菜单" :visible.sync="dialogFormVisible4EditFu">
                    <el-form ref="dataEditFormFu" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="title">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单级别">
                                    <el-select v-model="formData.level">
                                        <el-option label="父菜单" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="菜单说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4EditFu = false">取消</el-button>
                        <el-button type="primary" @click="handleEditFu()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            backend_url: "http://localhost:9002",
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisibleZi: false,//增加表单是否可见
            dialogFormVisibleFu: false,//增加表单是否可见
            dialogFormVisible4EditZi: false,//编辑表单是否可见
            dialogFormVisible4EditFu: false,//编辑表单是否可见
            rules: {//校验规则
                linkUrl: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            },
            items: []
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //添加子菜单
            handleAddZi() {
                this.$refs['dataAddFormZi'].validate((valid) => {
                    if (valid) {
                        axios.post(this.backend_url + "/menu/addMenu.do", this.formData).then((response) => {
                            if (response.data.flag) {
                                //请求成功
                                this.$message.success(response.data.message);
                            } else {
                                //请求失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            //刷新数据
                            this.findPage();
                            //隐藏新增窗口
                            this.dialogFormVisibleZi = false;
                        });
                    } else {
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                });
            },
            //添加父菜单
            handleAddFu() {
                this.$refs['dataAddFormFu'].validate((valid) => {
                    if (valid) {
                        axios.post(this.backend_url + "/menu/addMenu.do", this.formData).then((response) => {
                            if (response.data.flag) {
                                //请求成功
                                this.$message.success(response.data.message);
                            } else {
                                //请求失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            //刷新数据
                            this.findPage();
                            //隐藏新增窗口
                            this.dialogFormVisibleFu = false;
                        });
                    } else {
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                });
            },
            //编辑子菜单
            handleEditZi() {
                this.$refs["dataEditFormZi"].validate((valid) => {
                    if (valid) {
                        //数据校验成功,发起http请求
                        axios.post(this.backend_url + "/menu/editMenu.do", this.formData).then((response) => {
                            //判断是否成功
                            if (response.data.flag) {
                                //操作成功
                                this.$message.success(response.data.message);
                            } else {
                                //操作失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            this.findPage();
                            //隐藏新增窗口
                            this.dialogFormVisible4EditZi = false;
                        });
                    } else {
                        //数据校验失败
                        this.$message.error("数据校验失败");
                    }
                })
            },
            //编辑父菜单
            handleEditFu() {
                //数据校验成功,发起http请求
                axios.post(this.backend_url + "/menu/editMenu.do",this.formData).then((response) => {
                    //判断是否成功
                    if (response.data.flag) {
                        //操作成功
                        this.$message.success(response.data.message);
                    } else {
                        //操作失败
                        this.$message.error(response.data.message);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("网络异常");
                }).finally(() => {
                    this.findPage();
                    //隐藏新增窗口
                    this.dialogFormVisible4EditFu = false;
                });
            },
            //分页查询
            findPage(pageNo) {
                //如果传入页码则先更新模型中的页码
                if (pageNo) {
                    this.pagination.currentPage = pageNo;
                }
                //构造请求参数
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.get(this.backend_url + "/menu/findPage.do", {params: param}).then((response) => {
                    if (response.data.flag) {
                        this.dataList = response.data.data.rows;
                        this.pagination.total = response.data.data.total;
                    } else {
                        //请求失败
                        this.$message.error(response.data.message);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("网络异常");
                });

            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出子菜单添加窗口
            handleCreateZi() {
                this.resetForm();
                this.dialogFormVisibleZi = true;
                //查询父级菜单选项
                axios.get(this.backend_url + "/menu/findFuMenuNameAndId.do").then((response) => {
                    if (response.data.flag) {
                        this.items = response.data.data;
                    } else {
                        //失败消息提醒
                        this.$message.error(response.data.message);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("网络异常");
                });
            },
            //弹出父菜单
            handleCreateFu() {
                this.resetForm();
                this.dialogFormVisibleFu = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                if (row.level == 1) {
                    //若是父菜单项，则弹出父菜单编辑窗口
                    this.dialogFormVisible4EditFu = true;
                    this.formData = row;
                }
                if (row.level == 2) {
                    //若是子菜单项，则弹出子菜单编辑窗口
                    this.dialogFormVisible4EditZi = true;
                    this.formData = row;
                    //查询父级菜单选项
                    axios.get(this.backend_url + "/menu/findFuMenuNameAndId.do").then((response) => {
                        if (response.data.flag) {
                            this.items = response.data.data;
                        } else {
                            //失败消息提醒
                            this.$message.error(response.data.message);
                        }
                    }).catch((error) => {
                        console.log(error);
                        this.$message.error("网络异常");
                    });
                }
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                if(row.level==1){
                    this.$confirm("确认删除当前选中记录吗？", "提示", {type: 'warning'}).then(() => {
                        //点击确定按钮时只需此处代码
                        axios.post(this.backend_url + "/menu/delete.do?id=" + row.id).then((response) => {
                            if (response.data.flag) {
                                //成功
                                this.$message.success(response.data.message);
                                this.findPage();
                            } else {
                                // 失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        });
                    });
                }
                if (row.level == 2) {
                    this.$confirm("确认删除当前选中记录吗？", "提示", {type: 'warning'}).then(() => {
                        //点击确定按钮时只需此处代码
                        axios.post(this.backend_url + "/menu/delete.do?id=" + row.id).then((response) => {
                            if (response.data.flag) {
                                //成功
                                this.$message.success(response.data.message);
                                this.findPage();
                            } else {
                                // 失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        });
                    });
                }
            },
            showMessage(r) {
                if (r.response.status === 403) {
                    //权限不足
                    this.$message({
                        type: "error",
                        message: "你没有权限"
                    })
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }

        }
    })
</script>
</html>